import { mergeConfig, loadEnv } from 'vite';
import eslint from 'vite-plugin-eslint';
import { viteMockServe } from 'vite-plugin-mock';
import baseConfig from './vite.config.base';
import { include, exclude } from './plugin/optimize';
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  return mergeConfig(
    {
      mode: 'development',
      base: env.VITE_APP_BASE_URL,
      server: {
        proxy: {
          // '/api/tmgc2-production': {
          //     // 代理对应后端本地
          //     target: 'http://192.168.0.43:8645/',
          //     changeOrigin: true,
          //     logLevel: 'debug',
          //     ws: true,
          //     rewrite: path => {
          //         return path.replace('/api/tmgc2-production', '');
          //     },
          //     bypass(req, res, proxyRes) {
          //         //这里的req.url是经过路径重写后的url
          //         const realUrl =
          //             new URL(req.url || '', 'http://192.168.0.43:8645/')?.href || '';
          //         res.setHeader('x-real-url', realUrl);
          //     }
          // },
          '/api': {
            target: env.VITE_APP_API_ROOT,
            changeOrigin: true,
            logLevel: 'debug',
            ws: true,
            bypass(req, res, proxyRes) {
              //这里的req.url是经过路径重写后的url
              const realUrl = new URL(req.url || '', env.VITE_APP_API_ROOT)?.href || '';
              res.setHeader('x-res-proxyUrl', realUrl);
            },
            rewrite: path => path.replace('/api', '')
          },
          '/browser': {
            target: 'https://dev-skywalking.imefuture.com',
            changeOrigin: true,
            ws: true
          },
          '/v3': {
            target: 'https://dev-skywalking.imefuture.com',
            changeOrigin: true,
            ws: true
          }
        },
        open: true,
        port: 3000,
        host: '0.0.0.0',
        hmr: {
          overlay: false
        },
        fs: {
          strict: true
        }
      },
      optimizeDeps: { include, exclude },
      plugins: [
        viteMockServe({
          localEnabled: env.VITE_NODE_ENV === 'development' && env.VITE_APP_USE_MOCK === 'true',
          supportTs: true
        }),
        eslint({
          cache: false,
          include: ['src/**/*.ts', 'src/**/*.tsx', 'src/**/*.vue'],
          exclude: ['node_modules']
        })
      ]
    },
    baseConfig
  );
};
